/**
 * @description 请求配置 
*/
import axios from 'axios';
import qs from 'qs';
import { storageGet } from '@/utill/storage.js'

const { interceptors } = axios;

// 设置接口权限信息
let token = storageGet('token');
let userId = storageGet('userId');

axios.defaults.headers.common.Authorization = token || '';
axios.defaults.headers.common['User-Id'] = userId || '';


const interceptorUrl = config => {
  const { method, data } = config;
  if (method === 'post') {
    config.data = qs.stringify(data);
  }
  return config;
}


interceptors.request.use(function (config) {
  return interceptorUrl(config);
}, function (error) {
  return Promise.reject(error);
})

interceptors.response.use(function (response) {
  if (Number(response.data.code) === 500) {
    window.location.href = '/login';
    return;
  }
  return response;
}, function (error) {
  console.log(error);
  return error;
})

export default axios;